﻿#region File Description
//-----------------------------------------------------------------------------
// BusinessManagingScreen.cs
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ManagerGame;
using System.Collections.Generic;
using System.Diagnostics;
#endregion

namespace ManagerGame.Screens
{
    /// <summary>
    /// This is screen for managing all business
    /// </summary>
    class BusinessManagingScreen : MenuScreen
    {

        string menuTitle;
        Texture2D gradientTexture;
        Company companyManaged;
        Business business = Business.Instance;
        MenuEntry workersMenuEntry;
        MenuEntry salaryMenuEntry;

        // 
        static int[] amountOfWorkers = {1, 5, 10, 20 };
        static int currentAmountOfWorkers = 0; // this is index for the table "amountOfWorkers", so default is 1

        static int[] salary = { 30, 50, 100, 250, 500 };

        static int currentSalary = 0; // this is index for the salary - table

        public BusinessManagingScreen(string header, Company company):base(header)
        {
            companyManaged = company;
            // debugging:
            Debug.WriteLine("BusinessManagingScreen --- CompanyID:" + companyManaged.Id);

            menuTitle = header;
             // Create our business menu entries.
            workersMenuEntry = new MenuEntry(string.Empty);
            salaryMenuEntry = new MenuEntry(string.Empty);
            MenuEntry playGameMenuEntry = new MenuEntry("Done");



            SetMenuEntryText();


            // Hook up menu event handlers.
            workersMenuEntry.Selected += amountOfWorkersMenuEntrySelected;
            salaryMenuEntry.Selected += salaryMenuEntrySelected;
            playGameMenuEntry.Selected += doneMenuEntrySelected;

            IsPopup = false;


            MenuEntries.Add(workersMenuEntry);
            MenuEntries.Add(salaryMenuEntry);
            MenuEntries.Add(playGameMenuEntry);
        }

        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            workersMenuEntry.Text = "Workers: " + amountOfWorkers[currentAmountOfWorkers];
            salaryMenuEntry.Text = "Salary: " + salary[currentSalary] + " in a day / employee";
        }
      

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            GraphicsDevice graphics = ScreenManager.GraphicsDevice;

            // Darken down any other screens that were drawn beneath the popup.
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);



            Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192) * TransitionAlpha;


            Rectangle backgroundRectangle = new Rectangle();
            // Fade the popup alpha during transitions.
            Color color = Color.Green * TransitionAlpha;

            spriteBatch.Begin();

            // Draw the background rectangle.
            spriteBatch.Draw(gradientTexture, backgroundRectangle, color);


            spriteBatch.End();
        }
        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            if (Keyboard.GetState().IsKeyDown(Keys.X))
            {
                ExitScreen();
            }
        }

        public override void LoadContent()
        {
            base.LoadContent();
            ContentManager content = ScreenManager.Game.Content;
            gradientTexture = content.Load<Texture2D>("gradient");
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }


        void amountOfWorkersMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentAmountOfWorkers = (currentAmountOfWorkers + 1) % amountOfWorkers.Length;
           
            SetMenuEntryText();
            if (companyManaged == null)
            {
                return; // todo: "No company..."
            }
            companyManaged.Workers = amountOfWorkers[currentAmountOfWorkers];
            //ScreenManager.Initialize();

        }

        void salaryMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentSalary = (currentSalary + 1) % salary.Length;

            SetMenuEntryText();
            if (companyManaged == null)
            {
                return; // todo: "No company..."
            }
            companyManaged.Salary = salary[currentSalary];
        }

        void doneMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ExitScreen();
        }

    }
}
